using System.Diagnostics;
using System.IO;

namespace Coding4Fun.BoxingBots.Common
{
	public abstract class GamePacket
	{
		public byte[] ToByteArray()
		{
		    MemoryStream ms = new MemoryStream();
		    BinaryWriter bw = new BinaryWriter(ms);

			try
			{
				WritePacket(bw);
			}
			catch(IOException ex)
			{
				Debug.WriteLine("Error writing packet: " + ex);
			}

			return ms.ToArray();
		}

		public void FromBinaryReader(BinaryReader br)
		{
			try
			{
				ReadPacket(br);
			}
			catch(IOException ex)
			{
				Debug.WriteLine("Error reading packet: " + ex);
			}
		}

		public abstract void WritePacket(BinaryWriter bw);
		public abstract void ReadPacket(BinaryReader br);
	}
}